//
//  HotListViewController.m
//  DayByDay
//
//  Created by dllo on 16/5/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

// 热门歌单 推荐榜单

#import "HotListViewController.h"
#import "HotListCollectionViewCell.h"
#import "HotListModel.h"
#import <AFNetworking.h>
#import "PosterDetailViewController.h"
#import "MJRefresh.h"
@interface HotListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *myArray;
@property (nonatomic, assign)CGFloat currentOffSit_y;
@property (nonatomic, strong)JHJTabBarController *tabBar;
@property (nonatomic, assign)NSInteger page;

@end

@implementation HotListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myArray = [NSMutableArray array];
    self.navigationItem.title = @"歌单广场";
//    self.navigationController.navigationBar.translucent = NO;
    AppDelegate *tempAppdelegate = [[UIApplication sharedApplication] delegate];
    self.tabBar = tempAppdelegate.JHJTabBar;
    self.page = 1;
    [self createView];
    [self createData];
    [self createFooter];
}

- (void)createFooter {
    HotListViewController *hotVC = self;
    [self.collectionView addFooterWithCallback:^{
        hotVC.page++;
        [hotVC createData];
        
    }];
}

- (void)createHeader {
    
}

- (void)createData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *firstStr = @"http://search.dongting.com/songlist/search?q=tag:%E6%9C%80%E7%83%AD&";
    NSString *lastStr = @"&size=10&app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100";
    NSString *urlStr = [NSString stringWithFormat:@"%@page=%ld%@",firstStr, self.page, lastStr];
//    @"http://search.dongting.com/songlist/search?q=tag:%E6%9C%80%E7%83%AD&page=1&size=10&app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100"
    [manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        for (NSDictionary *dic in [responseObject valueForKey:@"data"]) {
            HotListModel *model = [[HotListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.myArray addObject:model];
        }
        [self.collectionView footerEndRefreshing];
        [self.collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}

- (void)createView {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, 40)];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor whiteColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 10, 50, 30);
    [button setImage:[UIImage imageNamed:@"global_icon_list_enter"] forState:UIControlStateNormal];
    [button setTitle:@"全部" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [button setImage:[UIImage imageNamed:@"global_icon_list_enter"] forState:UIControlStateNormal];
    button.tag = 10000;
    [view addSubview:button];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *hotButton = [UIButton buttonWithType:UIButtonTypeCustom];
    hotButton.frame = CGRectMake(WIDTH - 150, 10, 50, 30);
    [hotButton setTitle:@"最热" forState:UIControlStateNormal];
    [hotButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    hotButton.tag = 10001;
    [view addSubview:hotButton];
    [hotButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH -100, 10, 1, 20)];
    lineView.backgroundColor = [UIColor blackColor];
    [view addSubview:lineView];
    
    UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
    newButton.frame = CGRectMake(WIDTH - 90, 10, 50, 30);
    [newButton setTitle:@"最新" forState:UIControlStateNormal];
    newButton.tag = 10002;
    [newButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [view addSubview:newButton];
    [newButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 20;
    layout.itemSize = CGSizeMake((WIDTH - 50) / 2, (WIDTH - 50) / 2 + 50);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 114, WIDTH, HEIGHT - 40) collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[HotListCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([HotListCollectionViewCell class])];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
}



- (void)buttonAction:(UIButton *)button {
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    PosterDetailViewController *posterVC = [[PosterDetailViewController alloc] init];
    HotListModel *listModel = self.myArray[indexPath.row];
    posterVC._id = listModel._id;
    [self.navigationController pushViewController:posterVC animated:YES];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.myArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HotListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HotListCollectionViewCell class]) forIndexPath:indexPath];
    cell.model = self.myArray[indexPath.row];
    return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"%f",scrollView.contentOffset.y);
    if (scrollView.contentOffset.y > self.currentOffSit_y && scrollView.contentOffset.y <= 1394) {
        [self.tabBar hiddenTabBarWithAnimation:YES];
        self.currentOffSit_y = scrollView.contentOffset.y;
    } else if (scrollView.contentOffset.y < self.currentOffSit_y && scrollView.contentOffset.y > 0) {
        [self.tabBar showTabBarWithAnimation:YES];
        self.currentOffSit_y = scrollView.contentOffset.y;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
